package foundation.app;

import model.sys.RestResult;
import model.sys.RestfullResult;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

/**
 * @author futao
 * Created on 18-7-23-下午3:42.
 */
@Provider
public class RestfullResultWrapper implements MessageBodyWriter<RestfullResult> {
    @Override
    public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public long getSize(RestfullResult restfullResult, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    @Override
    public void writeTo(RestfullResult restfullResult, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
        RestResult restResult = new RestResult();
        restResult.setCode("0");
        restResult.setData(restfullResult);

        String s = ObjectToGsonString.parse(restResult);
        outputStream.write(s.getBytes("UTF-8"));
    }
}
